#!/usr/bin/env node

// 专门测试 build 版本的缓存机制
const { BriefingService } = require('./build/briefing-service.js');
const path = require('path');

async function testBuildVersionCache() {
  console.log('🔧 Testing Built Version Cache Mechanism');
  console.log('=' .repeat(50));

  const briefingService = new BriefingService();
  const testFile = path.resolve('example-docs/ai-guide-chinese.md');
  
  try {
    // 清理缓存
    await briefingService.clearBriefings();
    console.log('🧹 Cleared cache');

    console.log('\n=== First Call ===');
    const start1 = Date.now();
    const briefing1 = await briefingService.getBriefing(testFile);
    const time1 = Date.now() - start1;
    console.log(`⏱️  Time: ${time1}ms`);
    console.log(`📝 Length: ${briefing1.length} chars`);
    console.log(`📖 Preview: ${briefing1.substring(0, 80)}...`);

    console.log('\n=== Second Call (Immediate) ===');
    const start2 = Date.now();
    const briefing2 = await briefingService.getBriefing(testFile);
    const time2 = Date.now() - start2;
    console.log(`⏱️  Time: ${time2}ms`);
    console.log(`📝 Length: ${briefing2.length} chars`);
    console.log(`📖 Preview: ${briefing2.substring(0, 80)}...`);

    const isIdentical = briefing1 === briefing2;
    const speedup = time1 / time2;
    
    console.log('\n=== Analysis ===');
    console.log(`🎯 Content identical: ${isIdentical ? '✅' : '❌'}`);
    console.log(`⚡ Speed improvement: ${speedup.toFixed(1)}x`);
    console.log(`🔄 Cache working: ${isIdentical && speedup > 5 ? '✅' : '❌'}`);

    if (!isIdentical) {
      console.log('\n📊 Content comparison:');
      console.log(`First:  "${briefing1.substring(0, 120)}..."`);
      console.log(`Second: "${briefing2.substring(0, 120)}..."`);
    }

  } catch (error) {
    console.error('❌ Test failed:', error);
  }
}

if (require.main === module) {
  testBuildVersionCache().catch(console.error);
}
